import {takeEvery, select, call, put, take} from 'redux-saga/effects'
import axios from 'axios'
import { message } from 'antd';
// 引入自定义操作
import { GetUserBaseInfo } from '../actions/userinfo';
import { ChangeAboutMoneyActivate } from '../actions/aboutMoney';
// 引入状态和常量
import { RECHARGESAGA, WITHDRAWSAGA } from '../constants'
import { act } from '@testing-library/react';
import { API_URL } from '../../env';

// 提现
function* withDrawSaga(action) {
    console.log("withDrawSaga is ", action.data)
    const obj = action.data
    const res = yield call(axios.post, API_URL + "about_money/withdraw", {...obj}) 
    const res_data = res.data
    if (res_data.status == "200") {
        // 更新用户金币
        yield put(GetUserBaseInfo({money: res_data.user_money}))
        // 关闭弹窗
        yield put(ChangeAboutMoneyActivate({
            activate: false,
            is_recharge: false,
            is_withdraw: false
        }))
        // 显示成功
        message.success('已向您的账号提现：￥'+ String(res_data.to_user_money) + ", 扣除服务费：￥" + String(res_data.to_server_money))
    } else if (res_data.status != "500") {
        message.error("系统异常，请稍后再试")
    } else {
        message.error('输入密码或账号不正确，请认真检查再输入')
    }
}

// 充值
function* rechargeSaga(action) {
    const obj = action.data
    const res = yield call(axios.post, API_URL + "about_money/recharge", {...obj}) 
    const res_data = res.data
    if (res_data.status == "200") {
        // 更新用户金币
        yield put(GetUserBaseInfo({money: res_data.user_money}))
        // 关闭弹窗
        yield put(ChangeAboutMoneyActivate({
            activate: false,
            is_recharge: false,
            is_withdraw: false
        }))
        // 显示成功
        message.success('充值成功')
    } else {
        message.error('输入密码或账号不正确，请认真检查再输入')
    }
}


export function* aboutMoneySaga () {
    yield takeEvery(WITHDRAWSAGA, withDrawSaga)
    yield takeEvery(RECHARGESAGA, rechargeSaga)
}

